package controllers

import (
	"mdblog/models"
	"mdblog/models/auth"
)

const (
	AdminPrefixPath string = "/admin"
	AdminLoginPath         = AdminPrefixPath + "/login"
)

type AdminControllers struct {
	BaseController
}

func (o *AdminControllers) Prepare() {
	if o.Ctx.Request.RequestURI != AdminLoginPath {
		if u := o.GetSession("user"); u == nil {
			o.Redirect(AdminLoginPath, 302)
		} else {
			o.Data["User"] = u
		}
	}
	o.Data["Configs"], _ = models.GetAllConfigs()
}

func (o *AdminControllers) Login() {
	o.TplName = "admin/login2.html"
}

func (o *AdminControllers) Index() {
	o.TplName = "admin/index.html"
}

func (o *AdminControllers) Console() {
	o.TplName = "admin/console/console.html"
}

func (o *AdminControllers) Users() {
	o.TplName = "admin/auth/list.html"
}

func (o *AdminControllers) AccountCreate() {
	o.TplName = "admin/auth/create.html"
}

func (o *AdminControllers) AccountChangePassword() {
	id, _ := o.GetInt(":id")
	if id == 0 {
		o.Abort("404")
	}
	user, err := auth.GetUserFromId(id)
	if err != nil {
		if err == auth.UserDoesNotExist {
			o.Abort("404")
		} else {
			o.Abort("500")
		}
	}
	o.Data["ChangeUser"] = user
	o.TplName = "admin/auth/changepass.html"
}

func (o *AdminControllers) Role() {
	o.TplName = "admin/system/role.html"
}

func (o *AdminControllers) Power() {
	o.TplName = "admin/system/power.html"
}

func (o *AdminControllers) Message() {
	o.TplName = "admin/common/popper/message.html"
}

func (o *AdminControllers) Drawer() {
	o.TplName = "admin/common/popper/drawer.html"
}

func (o *AdminControllers) Loading() {
	o.TplName = "admin/common/popper/loading.html"
}

func (o *AdminControllers) Table() {
	o.TplName = "admin/common/senior/table.html"
}

func (o *AdminControllers) TreeTable() {
	o.TplName = "admin/common/senior/treeTable.html"
}

func (o *AdminControllers) Form() {
	o.TplName = "admin/common/basic/form.html"
}

func (o *AdminControllers) FormSelect() {
	o.TplName = "admin/common/basic/formSelects.html"
}

func (o *AdminControllers) Button() {
	o.TplName = "admin/common/basic/button.html"
}

func (o *AdminControllers) Error403() {
	o.TplName = "admin/error/403.html"
}

func (o *AdminControllers) Error404() {
	o.TplName = "admin/error/404.html"
}

func (o *AdminControllers) Error500() {
	o.TplName = "admin/error/500.html"
}
